<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .item{
            height: 100px;
            width: 100px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2 class="page-header">点击方块切换颜色</h2>
        <div class="item"> </div>
        <div class="item"> </div>
        <div class="item"> </div>
    </div>
    <script>
        //1.获取div元素对象
        let items = document.getElementsByClassName('item');

        //2.遍历并绑定事件
        for(var i = 0;i<items.length;i++){
            items[i].onclick = function(){
                //3.修改
                // this.style.background = 'pink';
                console.log(i);
                items[2].style.background = 'pink';
            }
        }
    </script>
</body>
</html>